/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fichiers;

import java.awt.BorderLayout;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.util.EventObject;
import javax.swing.AbstractCellEditor;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeCellEditor;

/**
 *
 * @author H
 */
public class MyDefaultTreeCellEditor extends AbstractCellEditor
        implements  TreeCellEditor, ActionListener {

    private JPanel myPanel;
    private JTextField myText;

    public MyDefaultTreeCellEditor() {
        
        myPanel = new JPanel();
        myText = new JTextField(10);
        myText.addActionListener(this);
        myPanel.setLayout(new BorderLayout());
        myPanel.add(BorderLayout.CENTER, myText);
        
    }

    public Component getTreeCellEditorComponent(JTree tree, Object value,
            boolean isSelected, boolean expanded, boolean leaf, int row) {
        if (value instanceof DefaultMutableTreeNode) {
            DefaultMutableTreeNode ob = (DefaultMutableTreeNode) value;
            myText.setText(ob.getUserObject().toString());
        }
        return myPanel;
    }

    public Object getCellEditorValue() {
        return myText.getText();
    }
    
    public boolean isCellEditable(EventObject anEvent) {               
	    if (anEvent instanceof MouseEvent) { 
                return ((MouseEvent)anEvent).getClickCount() >= 3;
	    }
	    return true;
	}

    public void actionPerformed(ActionEvent e) {
        this.stopCellEditing();
    }
}

